<?php

class Position extends Component {
	var $params = array (
		'title',
		'template',
		'comId',
		'comClass',
	);	
	public static $actions = array ();
	
	function __construct ($id = false, $params = array ()) {
		$this->id = $id;
		
		$this->template = false;
		$this->comId = false;
		$this->comClass = false;
		
		if ($this->id != false) {
			$this->load ();
		}
		else {
			foreach ($params as $key=>$value) {
				$this->{$key} = $value;
			}
		}
	}
	
	function createTable () {
		global $dbh;
  
  		$tableName = get_class ($this);
		
		$query = <<<QUERY
CREATE TABLE `{$tableName}` (
  `id` char(32) NOT NULL,
  `title` varchar(100) NOT NULL,
  `template` varchar(50),
  `comId` char(32),
  `comClass` varchar(50),
  PRIMARY KEY  (`id`),
  KEY `title_index` (`title`),
  KEY `comId_comClass_index` (`comId`, `comClass`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8
QUERY;
		$dbh->exec ($query);
		
		return true;
	}
	
	function assignComponent ($target, $template = false) {
		$this->comId = $target->id;
		$this->comClass = get_class ($target);
		if ($template) {
			$this->template = $template;
		}
		else {
			$this->template = $target->template;
		}
	}
	
	function unassignComponent() {
		$this->comId = false;
		$this->comClass = false;
		$this->template = false;
	}
	
	function getComponent () {
		if ($this->comId && $this->comClass) {
			$ret = eval ("return new {$this->comClass} ('{$this->comId}');");
			$ret->template = $this->template;
			return $ret;
		}
		else {
			return false;
		}
	}
	
	function render () {
		if (!isset($this->component)) {
			$this->component = $this->getComponent ();
		}
		$this->component->container = $this->container;
		$this->component->render ();
	}
};

?>